﻿
using Furion.FriendlyException;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// 扩展方法
/// </summary>
public static class Extends
{
    /// <summary>
    /// 将金额转成 分制 用于微信支付
    /// </summary>
    /// <param name="price"></param>
    /// <returns></returns>
    public static int ToUnitMoney(this double price)
    {
        return (int)(price * 100);
    }

    public static int ToUnitMoney(this decimal price)
    {
        return (int)(price * 100);
    }

    /// <summary>
    /// 深度拷贝对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T DeepCopy<T>(this T t)
    {
        //1.将对象序列化成json字符串
        string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(t);

        return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonStr) ?? throw Oops.Oh("DEEP COPY ERROR");
    }

    /// <summary>
    /// 格式化时间
    /// </summary>
    /// <param name="time"></param>
    /// <param name="longTimeFmt">是否将长期日期转换为yyyy-MM-dd格式</param>
    /// <returns></returns>
    public static string FmtTime(this string? time, bool longTimeFmt = false)
    {
        if (string.IsNullOrEmpty(time)) throw Oops.Oh("错误的时间格式");

        string res = time.Replace(".", "-").Replace("年", "-").Replace("月", "-").Replace("日", "");

        if (longTimeFmt && time == "长期") return "2999-12-31";

        return res;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToJson(this object obj)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="json"></param>
    /// <returns></returns>
    public static T ToObject<T>(this string json)
    {
        return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json) ?? throw Oops.Oh("序列化失败");
    }

    /// <summary>
    /// 获取当前的时间戳
    /// </summary>
    /// <returns></returns>
    public static long GetTimeSpan()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

        return (long)ts.TotalSeconds;
    }

    /// <summary>
    /// 获取当前时间戳
    /// </summary>
    /// <returns></returns>
    public static long GetTimeSpan13()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

        return (long)ts.TotalMilliseconds;
    }

    /// <summary>
    /// 转base64
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string toBase64(this string str)
    {
        byte[] data = System.Text.Encoding.UTF8.GetBytes(str);

        return Convert.ToBase64String(data);
    }

    /// <summary>
    /// base64解码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string decodeBase64(this string str)
    {
        byte[] base64EncodedBytes = Convert.FromBase64String(str);

        return Encoding.UTF8.GetString(base64EncodedBytes);
    }

    /// <summary>
    /// 获取一个以时间和随机数的订单号
    /// </summary>
    /// <returns></returns>
    public static string GetOrderNo()
    {
        string orderNo = DateTime.Now.ToString("yyyyMMddHHmmssffff") + new Random().Next(111111, 999999);

        return orderNo;
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string sha256(this string input)
    {
        return sha256(Encoding.UTF8.GetBytes(input));
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string sha256(this byte[] input)
    {
        using (SHA256 sha256Hash = SHA256.Create())
        {
            byte[] bytes = sha256Hash.ComputeHash(input);

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < bytes.Length; i++)
            {
                builder.Append(bytes[i].ToString("x2"));
            }

            return builder.ToString();
        }
    }
}

